package com.ztc.ZtcApp.dao;
import com.ztc.ZtcApp.bean.ZtcApp;
import com.ztc.ZtcClasses.bean.ZtcClasses;
import java.util.List;
import org.springframework.stereotype.Repository;
import com.aqgj.util.BaseDaoImpl;
/**
 * <p>
 *
 * </p>
 *
 * @ClassName ZtcAppDao
 * @JDK 1.5
 * @author
 * @date
 */
@Repository("ZtcAppDao")
public class ZtcAppDao extends BaseDaoImpl<ZtcApp> {
    private int pageSize = 10;
    public String save(ZtcApp ztcApp) {
        this.getHibernateTemplate().save(ztcApp);
        return ztcApp.getId();
    }

    public String delete(String  ztcAppId) {
        String sql = "delete from ztc_app where 1=1";
        if (ztcAppId != null) {
            sql += " and id='" + ztcAppId + "'";
        }
        if (this.currentSession().createNativeQuery(sql, ZtcClasses.class).executeUpdate() >= 0) {
            return "1";
        }
        return "0";
    }

    public List<ZtcApp> searchInfoEquals(ZtcApp ztcApp) {
        String sql = "select * from ztc_app  where 1=1";
        if (ztcApp.getId() != null) {
            sql += " and id='" + ztcApp.getId() + "'";
        }
        if (ztcApp.getAppVersion() != null) {
            sql += " and app_version='" + ztcApp.getAppVersion() + "'";
        }
        if (ztcApp.getName() != null) {
            sql += " and name='" + ztcApp.getName() + "'";
        }
        if (ztcApp.getAppAddress() != null) {
            sql += " and app_address='" + ztcApp.getAppAddress()  + "'";
        }
        if (ztcApp.getUploadDate() != null) {
            sql += " and upload_date='" + ztcApp.getUploadDate() + "'";
        }
        if (ztcApp.getUploadUserId() != null) {
            sql += " and upload_user_id='" + ztcApp.getUploadUserId() + "'";
        }
        sql += " order by upload_date";
        List<ZtcApp> list = this.currentSession().createNativeQuery(sql, ZtcApp.class).getResultList();
        return list;
    }

    public List<ZtcApp> searchInfoLikes(String pageNumber) {
        String sql = "select * from ztc_app order by upload_date desc";
        int startIndex = 0;
        int endIndex = 10;
        if (pageNumber == null) {
            pageNumber = "0";
        } else {
            int number = Integer.parseInt(pageNumber);
            if (!"0".equals(pageNumber)) {
                number = number - 1;
            }
            startIndex = (number) * pageSize;
        }
        sql += " limit " + startIndex + "," + endIndex;
        List<ZtcApp> list = this.currentSession().createNativeQuery(sql, ZtcApp.class).getResultList();
        return list;
    }
    public List<ZtcApp> searchToUpdate(String appVersion) {
        String sql = "select * from ztc_app where ztc_app.app_version='"+appVersion+"'";
        List<ZtcApp> list = this.currentSession().createNativeQuery(sql, ZtcApp.class).getResultList();
        return list;
    }
    public int getCount() {
        String sql = "select count(*) from ztc_app order by upload_date desc";
        @SuppressWarnings("unchecked")
        List<Object> li = this.getHibernateTemplate().getSessionFactory().getCurrentSession().createNativeQuery(sql)
                .getResultList();
        return new Integer(String.valueOf(li.get(0)));
    }

    public String update(ZtcApp ztcApp) {
        this.getHibernateTemplate().saveOrUpdate(ztcApp);
        if (ztcApp.getId() != null) {
            return "1";
        }
        return "0";
    }

    public List<ZtcApp> getListOrderByVersion() {
        String sql = "select * from ztc_app order by app_version desc limit 0,1";
        List<ZtcApp> list = this.currentSession().createNativeQuery(sql, ZtcApp.class).getResultList();
        return list;
    }
}